const config = require("../config")
const logger = require("../utils/logger")
const mysqlUtil = require("../utils/mysqlUtil")
const JSZip = require('jszip')
const Docxtemplater = require('docxtemplater')
const ImageModule = require('open-docxtemplater-image-module')
const fs = require("fs")
const fsp = require('fs').promises;
const path = require("path")
const moment = require('moment')
const libre = require('libreoffice-convert');
libre.convertAsync = require('util').promisify(libre.convert);

async function genWord(tempname, data, name, id) {
    var content = fs.readFileSync( path.resolve(__dirname, "../template/" + tempname), "binary" )
    var zip = new JSZip(content)
    var doc = new Docxtemplater()
    var opts = {
        centered: false,
        getImage: function (tagValue, tagName) {
            console.log('get image', __dirname, tagValue, tagName)
            let filename = path.basename(tagValue)
            return fs.readFileSync(path.join(__dirname, '../static/' + filename));
        },
        getSize: function (img, tagValue, tagName) {
            return [64, 32];
        }
    }
    doc.attachModule(new ImageModule(opts))
    doc.loadZip(zip)

    doc.setData(data)

    logger.debug('data', data)

    doc.render()

    var buf = doc.getZip().generate({ type: 'nodebuffer' });
    let fileName = `${name}-${id}-${moment().format('YYYYMMDDHHmmss')}`
    fs.writeFileSync(path.resolve(__dirname, '../static/' + fileName + '.docx'), buf)

    const ext = '.pdf'
    const inputPath = path.join(__dirname, '../static/' + fileName + '.docx');
    const outputPath = path.join(__dirname, '../static/' + fileName + '.pdf');

    // Read file
    const docxBuf = await fsp.readFile(inputPath);

    // Convert it to pdf format with undefined filter (see Libreoffice docs about filter)
    let pdfBuf = await libre.convertAsync(docxBuf, ext, undefined);
    
    // Here in done you have pdf file which you can save or transfer in another stream
    await fsp.writeFile(outputPath, pdfBuf);

    return fileName + '.pdf'
}

async function vacationPdf(uid, id) {

    if (!id) return {code: -1, message: '参数缺失'}

    try {
        let res = await mysqlUtil.execute(`
            SELECT
                tb_user_vacation_apply.*,
                tb_user.name,
                con_department.name departName
            FROM 
                tb_user_vacation_apply
            LEFT JOIN tb_user ON tb_user.uid = tb_user_vacation_apply.uid
            LEFT JOIN con_department ON con_department.id = tb_user.department
            WHERE tb_user_vacation_apply.id = ? LIMIT 1
        `, [id])
        if (res.length == 0) {
            return {code: -1, message: '不存在'}
        }

        // 申请信息
        let applyInfo = res[0]
        logger.debug('apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.departName,
            applyname: applyInfo.name,
            riqi: moment(applyInfo.createTime).format('YYYY-MM-DD'),
            leixing: applyInfo.leixing,
            reason: applyInfo.reason,
            outYancheng: applyInfo.outYancheng,
            start: moment(applyInfo.startDate).format('YYYY-MM-DD HH:mm:ss'),
            end: moment(applyInfo.endDate).format('YYYY-MM-DD HH:mm:ss'),
            hour: applyInfo.days,
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('qingjia.docx', data, '请假申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function caigouPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_caigou_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('caigou apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            fangshi: applyInfo.fangshi,
            leibie: applyInfo.leibie,
            applyTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.department,
            items: applyInfo.items || [],
            totalMoney: applyInfo.totalMoney,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('caigou.docx', data, '采购申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function chuchaiPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_chuchai_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('chuchaiPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 申请人信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.uid])
        let applyName = res[0].name

        if (applyInfo.chuchairen) {
            let names = []
            for (let i = 0; i != applyInfo.chuchairen.length; i++) {
                names.push(applyInfo.chuchairen[i].name)
            }
            applyName = names.join(',')
        }

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            applyName: applyName,
            department: applyInfo.department,
            start: moment(applyInfo.startDate).format('YYYY-MM-DD'),
            end: moment(applyInfo.endDate).format('YYYY-MM-DD'),
            days: applyInfo.days,
            reason: applyInfo.reason,
            chufa: applyInfo.chufa,
            destination: applyInfo.destination,
            bao: (applyInfo.bao || []).join(),
            jiaotong: applyInfo.jiaotong,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('chuchai.docx', data, '出差申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function jiabanPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_jiaban_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('jiabanPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 负责人信息
        let fuzeren = ''
        if (applyInfo.fuzeren) {
            res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.fuzeren])
            fuzeren = res[0].name
        }

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let attenders = ''
        applyInfo.attenders = applyInfo.attenders || []
        for (let i = 0; i != applyInfo.attenders.length; i++) {
            let rrrr = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.attenders[i].uid])
            if (rrrr.length != 0) {
                let name = rrrr[0].name
                attenders += name
                if (i != applyInfo.attenders.length - 1) {
                    attenders += '、'
                }
            }
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.department,
            fuzeren: fuzeren,
            start: moment(applyInfo.startTime).format('YYYY-MM-DD HH:mm:ss'),
            end: moment(applyInfo.endTime).format('YYYY-MM-DD HH:mm:ss'),
            shichang: applyInfo.shichang,
            reason: applyInfo.reason,
            attenders,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('jiaban.docx', data, '加班申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function ruzhiPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_entry_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('ruzhiPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let chengbanren = ''
        if (applyInfo.chengbanren && applyInfo.chengbanren.chengbanren0) {
            chengbanren = applyInfo.chengbanren.chengbanren0.name || ''
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            name: applyInfo.name,
            gonghao: applyInfo.gonghao,
            department: applyInfo.department,
            position: applyInfo.position,
            upper: applyInfo.upper,
            leixing: applyInfo.leixing,
            xingzhi: applyInfo.xingzhi,
            ruzhiDate: applyInfo.ruzhiDate,
            phone: applyInfo.phone,
            email: applyInfo.email,
            gender: applyInfo.gender,
            remark: applyInfo.remark,
            chengbanren,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('ruzhi.docx', data, '入职申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function zhuanzhengPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_zhuanzheng_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('zhuanzhengPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            name: applyInfo.zhuanzhengUserName,
            uid: applyInfo.zhuanzhengUser,
            ruzhiDate: moment(applyInfo.ruzhiDate).format('YYYY-MM-DD'),
            shiyongqi: applyInfo.shiyongqi,
            zhuanzhengDate: moment(applyInfo.zhuanzhengDate).format('YYYY-MM-DD'),
            position: applyInfo.position || '',
            department: applyInfo.department || '',
            upper: applyInfo.upper || '',
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('zhuanzheng.docx', data, '转正申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function lizhiPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_departure_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('lizhiPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            name: applyInfo.name,
            lizhiDate: moment(applyInfo.lizhiDate).format('YYYY-MM-DD'),
            receiverName: applyInfo.receiverName || '',
            jobDesc: applyInfo.jobDesc,
            itemDesc: applyInfo.itemDesc,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('lizhi.docx', data, '离职交接', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function dubandanPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_duban_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('dubandanPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 责任人
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.dutyOfficer])
        let zerenren = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            title: applyInfo.title,
            dutyDepart: applyInfo.dutyDepart,
            zerenren,
            content: applyInfo.content,
            dubanTime: applyInfo.dubanTime,
            deadTime: applyInfo.deadTime,
            dubanDepart: applyInfo.dubanDepart,
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('dubandan.docx', data, '督办单', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function huiyishiPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_meeting_room_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('huiyishiPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let attenders = ''
        applyInfo.attenders = applyInfo.attenders || []
        for (let i = 0; i != applyInfo.attenders.length; i++) {
            let rrrr = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.attenders[i].uid])
            if (rrrr.length != 0) {
                let name = rrrr[0].name
                attenders += name
                if (i != applyInfo.attenders.length - 1) {
                    attenders += '、'
                }
            }
        }

        let chengbanren = ''
        if (applyInfo.chengbanren && applyInfo.chengbanren.chengbanren0) {
            chengbanren = applyInfo.chengbanren.chengbanren0.name
        }

        let jutiChengbanren = ''
        if (applyInfo.dabaoren && applyInfo.dabaoren.length > 0) {
            jutiChengbanren = applyInfo.dabaoren[0].name
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.department,
            riqi: moment(applyInfo.startTime).format('YYYY-MM-DD'),
            period: moment(applyInfo.startTime).format('HH:mm') + '-' + moment(applyInfo.endTime).format('HH:mm'),
            attenders,
            renshu: applyInfo.renshu,
            topic: applyInfo.topic,
            room: applyInfo.room,
            equips: (applyInfo.equips || []).join(),
            items: (applyInfo.items || []).join(),
            remark: applyInfo.remark,
            auditors,
            chengbanren,
            jutiChengbanren,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('huiyishi.docx', data, '会议室申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function jiedaiPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_jiedai_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('jiedaiPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let peitong = ''
        applyInfo.peitongrenyuan = applyInfo.peitongrenyuan || []
        for (let i = 0; i != applyInfo.peitongrenyuan.length; i++) {
            let rrrr = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.peitongrenyuan[i].uid])
            if (rrrr.length != 0) {
                let name = rrrr[0].name
                peitong += name
                if (i != applyInfo.peitongrenyuan.length - 1) {
                    peitong += '、'
                }
            }
        }

        let dabaoren = ''
        applyInfo.dabaoren = applyInfo.dabaoren || []
        for (let i = 0; i != applyInfo.dabaoren.length; i++) {
            let rrrr = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.dabaoren[i].uid])
            if (rrrr.length != 0) {
                let name = rrrr[0].name
                dabaoren += name
                if (i != applyInfo.dabaoren.length - 1) {
                    dabaoren += '、'
                }
            }
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.department,
            start: moment(applyInfo.startTime).format('YYYY-MM-DD'),
            end: moment(applyInfo.endTime).format('YYYY-MM-DD'),
            guestCompany: applyInfo.guestCompany,
            guestCnt: applyInfo.guestCnt,
            peitong,
            dabaoren,
            peitongrenshu: applyInfo.peitongrenshu,
            reason: applyInfo.reason,
            remark: applyInfo.remark,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('jiedai.docx', data, '接待单申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function jiayouPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_jiayou_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('jiayouPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 申请人信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.uid])
        let applyName = res[0].name

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.department,
            name: applyName,
            riqi: applyInfo.jiayouDate,
            chepai: applyInfo.chepai,
            lastLicheng: applyInfo.lastLicheng,
            wanzheng: applyInfo.wanzheng == 0 ? '否' : '是',
            pics,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('jiayou.docx', data, '车辆加油', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function yongchePdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_yongche_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('yongchePdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 申请人信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.uid])
        let applyName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            department: applyInfo.department,
            name: applyName,
            riqi: moment(applyInfo.startTime).format('YYYY-MM-DD'),
            reason: applyInfo.reason,
            jiashiren: applyInfo.jiashirenName || '',
            routes: applyInfo.routes,
            start: moment(applyInfo.startTime).format('YYYY-MM-DD HH:mm:ss'),
            end: moment(applyInfo.endTime).format('YYYY-MM-DD HH:mm:ss'),
            chepai: applyInfo.chepai,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('yongche.docx', data, '用车申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function yongyinPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_yongyin_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('yongyinPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 申请人信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.uid])
        let applyName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            bianhao: applyInfo.bianhao,
            department: applyInfo.department,
            yongyinTime: moment(applyInfo.yongyinTime).format('YYYY-MM-DD HH:mm:ss'),
            applyName,
            fenshu: applyInfo.fenshu,
            wenjianming: applyInfo.wenjianming,
            reason: applyInfo.reason,
            yinzhang: (applyInfo.yinzhang || []).join(),
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('yongyin.docx', data, '用印申请', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function wuzilingyongPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_wuzilingyong_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('wuzilingyongPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 申请人信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.uid])
        let applyName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            applyName: applyName,
            mingcheng: applyInfo.mingcheng,
            pinpai: applyInfo.pinpai,
            guige: applyInfo.guige,
            jiliang: applyInfo.jiliang,
            shuliang: applyInfo.shuliang,
            yongtu: applyInfo.yongtu,
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('wuzilingyong.docx', data, '物资领用', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function hetongPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_hetong_shenpi WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('hetongPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            bianhao: applyInfo.bianhao,
            mingcheng: applyInfo.mingcheng,
            qianyueriqi: moment(applyInfo.qianyueriqi).format('YYYY-MM-DD'),
            wofangdanwei: applyInfo.wofangdanwei,
            wofangfuzeren: applyInfo.wofangfuzeren,
            duifangdanwei: applyInfo.duifangdanwei,
            duifangfuzeren: applyInfo.duifangfuzeren,
            fawuyijian: applyInfo.fawuYijian,
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('hetong.docx', data, '合同审批', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function zhaobiaoPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_zhaobiao_shenhe WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('zhaobiaoPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 申请人信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.uid])
        let applyName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        // 负责人信息
        let fuzeren = ''
        if (applyInfo.fuzeren) {
            res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.fuzeren])
            fuzeren = res[0].name
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            bianhao: applyInfo.bianhao,
            mingcheng: applyInfo.mingcheng,
            kongzhijia: applyInfo.kongzhijia,
            caigou: applyInfo.caigou,
            danwei: applyInfo.danwei,
            start: applyInfo.startDate,
            end: applyInfo.endDate,
            jiaofu: applyInfo.jiaofuDate,
            fuzeren,
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('zhaobiao.docx', data, '招标审核', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function chuanyuePdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_chuanyue_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('chuanyuePdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }
        for (let key in applyInfo.chengbanren) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.chengbanren[key].positionName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.chengbanren[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.chengbanren[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.chengbanren[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.chengbanren[key].ts) {
                auditorInfo.time = moment(applyInfo.chengbanren[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.chengbanren[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let pics = []
        applyInfo.pics = applyInfo.pics || []
        for (let i = 0; i != applyInfo.pics.length; i++) {
            pics.push({a: '', pic: applyInfo.pics[i], b: ''})
        }

        let readers = ''
        applyInfo.readers = applyInfo.readers || []
        for (let i = 0; i != applyInfo.readers.length; i++) {
            let rrrr = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.readers[i].uid])
            if (rrrr.length != 0) {
                let name = rrrr[0].name
                readers += name
                if (i != applyInfo.readers.length - 1) {
                    readers += '、'
                }
            }
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            bianhao: applyInfo.bianhao,
            danwei: applyInfo.danwei,
            wenhao: applyInfo.wenhao,
            biaoti: applyInfo.biaoti,
            shouwenDate: applyInfo.shouwenDate,
            zhaiyao: applyInfo.zhaiyao,
            readers,
            pics,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('chuanyue.docx', data, '传阅文件', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

async function liuzhuanPdf(uid, id) {
    try {
        let res = await mysqlUtil.execute('SELECT * FROM tb_user_liuzhuan_apply WHERE id = ? LIMIT 1', [id])
        if (res.length == 0) return {code: -1, message: '不存在'}

        let applyInfo = res[0]
        logger.debug('liuzhuanPdf apply info', applyInfo)

        // 我的信息
        res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
        let myName = res[0].name

        // 审核人信息
        let auditors = []
        for (let key in applyInfo.auditors) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.auditors[key].stepName || applyInfo.auditors[key].positionName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.auditors[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.auditors[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.auditors[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.auditors[key].ts) {
                auditorInfo.time = moment(applyInfo.auditors[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.auditors[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }
        for (let key in applyInfo.chengbanren) {
            let auditorInfo = {}
            auditorInfo.a = ''
            auditorInfo.stepname = applyInfo.chengbanren[key].positionName || ''
            auditorInfo.agree = '未审批'
            if (applyInfo.chengbanren[key].sta == 1) {
                auditorInfo.agree = '已同意'
            } else if (applyInfo.chengbanren[key].sta == 2) {
                auditorInfo.agree = '已拒绝'
            }
            auditorInfo.msg = applyInfo.chengbanren[key].msg || ''
            auditorInfo.time = ''
            if (applyInfo.chengbanren[key].ts) {
                auditorInfo.time = moment(applyInfo.chengbanren[key].ts).format('YYYY-MM-DD HH:mm:ss')
            }
            // 获取审核人信息
            let rrr = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [applyInfo.chengbanren[key].uid])
            auditorInfo.auditorname = rrr[0].name
            auditorInfo.sign = rrr[0].signimg

            auditors.push(auditorInfo)
        }

        let data = {
            createTime: moment(applyInfo.createTime).format('YYYY-MM-DD HH:mm:ss'),
            bianhao: applyInfo.bianhao,
            danwei: applyInfo.danwei,
            wenhao: applyInfo.wenhao,
            biaoti: applyInfo.biaoti,
            chengwenDate: moment(applyInfo.chengwenDate).format('YYYY-MM-DD'),
            zhaiyao: applyInfo.zhaiyao,
            attaches: applyInfo.attaches,
            auditors,
            printTime: moment().format('YYYY-MM-DD HH:mm:ss'),
            printer: myName
        }

        let fileName = await genWord('liuzhuan.docx', data, '公文流转', id)

        return {code: 0, message: 'success', data: config.baseUrl + fileName}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

module.exports = {
    printPdf: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        logger.debug('print pdf', params)
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            if (params.typ == 0) { // 请假
                return callback(await vacationPdf(uid, params.id))
            }
            else if (params.typ == 1) { // 出差
                return callback(await chuchaiPdf(uid, params.id))
            }
            else if (params.typ == 2) { // 加班
                return callback(await jiabanPdf(uid, params.id))
            }
            else if (params.typ == 3) { // 入职
                return callback(await ruzhiPdf(uid, params.id))
            }
            else if (params.typ == 4) { // 转正
                return callback(await zhuanzhengPdf(uid, params.id))
            }
            else if (params.typ == 5) { // 离职
                return callback(await lizhiPdf(uid, params.id))
            }
            else if (params.typ == 6) { // 督办单
                return callback(await dubandanPdf(uid, params.id))
            }
            else if (params.typ == 7) { // 会议室
                return callback(await huiyishiPdf(uid, params.id))
            } 
            else if (params.typ == 8) { // 接待单
                return callback(await jiedaiPdf(uid, params.id))
            }
            else if (params.typ == 9) { // 加油
                return callback(await jiayouPdf(uid, params.id))
            }
            else if (params.typ == 10) { // 用车
                return callback(await yongchePdf(uid, params.id))
            } 
            else if (params.typ == 11) { // 用印
                return callback(await yongyinPdf(uid, params.id))
            }
            else if (params.typ == 13) { // 采购
                return callback(await caigouPdf(uid, params.id))
            }
            else if (params.typ == 14) { // 物资领用
                return callback(await wuzilingyongPdf(uid, params.id))
            }
            else if (params.typ == 15) { // 合同审批
                return callback(await hetongPdf(uid, params.id))
            }
            else if (params.typ == 16) { // 招标审核
                return callback(await zhaobiaoPdf(uid, params.id))
            }
            else if (params.typ == 17) { // 传阅文件
                return callback(await chuanyuePdf(uid, params.id))
            }
            else if (params.typ == 18) { // 公文流转
                return callback(await liuzhuanPdf(uid, params.id))
            }
             else {
                callback({code: -1, message: '参数错误'})
            }
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    }

}